package com.lzw.bluetoothpractice.Adapters;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.content.Context;
import android.nfc.Tag;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lzw.bluetoothpractice.Activitys.SearchDeviceActivity;
import com.lzw.bluetoothpractice.R;

import java.util.List;


public class BLEAdapters extends RecyclerView.Adapter<BLEAdapters.ViewHolder> {


    private List<BluetoothDevice> mDatas;
    private List<Integer> mRssis;
    private Context mcontext;
    private View itemView;
    private RecyclerView rvIist;
    private OnRecyclerItemClickListener mOnItemClickListener=null;//声明自定义的接口




    public BLEAdapters(List<BluetoothDevice> mDatas, List<Integer> mRssis, Context mcontext, RecyclerView rvIist) {
        this.mDatas = mDatas;
        this.mRssis = mRssis;
        this.mcontext = mcontext;
        this.rvIist = rvIist;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        BluetoothDevice device = (BluetoothDevice) getItem(position);
        holder.tvBlueName.setText(device.getName());
       // Log.e("ble_tag","Name:"+device.getName());
        holder.tvBlueMsg.setText(device.getAddress());
        holder.rssi.setText(mRssis.get(position)+"");
        if (mOnItemClickListener != null) {
            holder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onClick(v, position);
                }
            });
        }
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        itemView= LayoutInflater.from(mcontext).inflate(R.layout.bluetooth,parent,false);
       // itemView.setTag(viewType);
        ViewHolder viewHolder=new ViewHolder(itemView);

        return viewHolder;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public enum ViewName {
        ITEM,
        PRACTISE
    }
    public interface OnRecyclerItemClickListener   {
        void onClick(View view,int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvBlueName;
        TextView tvBlueMsg;
        TextView rssi;
        View view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view=itemView;
            tvBlueName=itemView.findViewById(R.id.tvBlueName);
            tvBlueMsg=itemView.findViewById(R.id.tvBlueMsg);
            rssi=itemView.findViewById(R.id.rssi);

        }
    }
    //定义方法并传给外面的使用者
    public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener  = listener;
    }

}
